package com.cloud.logger.zchain.zchainserver.service;

import com.cloud.logger.zchain.core.common.base.ResultMessage;
import com.cloud.logger.zchain.core.common.constant.ResultCodeConstant;
import com.cloud.logger.zchain.core.exception.BusinessException;
import com.cloud.logger.zchain.zchainserver.entity.AngelaLoggerProject;
import com.cloud.logger.zchain.zchainserver.entity.ProjectUserRelation;
import com.cloud.logger.zchain.zchainserver.entity.ZchainUser;
import com.cloud.logger.zchain.zchainserver.mongo.AngelaProjectRepository;
import com.cloud.logger.zchain.zchainserver.mongo.ProjectUserRelationRepository;
import com.cloud.logger.zchain.zchainserver.mongo.ZchianUserRepository;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

/**
 * User: wcy
 * Date: 2017/11/30
 * Time: 13:24
 */
@Service
public class ProjectService {

    @Resource
    private ZchianUserRepository zchianUserRepository;

    @Resource
    private ProjectUserRelationRepository projectUserRelationRepository;

    @Resource
    private AngelaProjectRepository angelaProjectRepository;

    public List<ZchainUser> getUsersByProjectId(String id){
        List<ProjectUserRelation> projectUserRelations = projectUserRelationRepository.findByProjectId(id);

        List<CompletableFuture<ZchainUser>> futures = projectUserRelations.
                stream().map(p -> CompletableFuture.supplyAsync(()-> getUser(p.getUserId()))).collect(Collectors.toList());

        List<ZchainUser> zchainUsers = futures.stream().map(CompletableFuture::join).collect(Collectors.toList());

        return zchainUsers;
    }

    private ZchainUser getUser(String userId) {
        return zchianUserRepository.findOne(userId);
    }

    public ResultMessage save(AngelaLoggerProject project) throws BusinessException {
        ResultMessage resultMessage = new ResultMessage();
        AngelaLoggerProject findProject = angelaProjectRepository.findOneByCode(project.getCode());
        if(findProject!=null)
            throw new BusinessException("当前编码的项目管理员已经创建，如果需要请向管理员申请权限！！");
        project = angelaProjectRepository.save(project);
        if (project.getId() !=null){
            resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
            resultMessage.setId(project.getId()+"");
        }else{
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
        }
        return resultMessage;
    }

    public ResultMessage updateProject(AngelaLoggerProject project) throws BusinessException {
        ResultMessage resultMessage = new ResultMessage();
        AngelaLoggerProject findProject = angelaProjectRepository.findOneByCodeAndIdNot(project.getCode(),project.getId());
        if(findProject!=null)
            throw new BusinessException("当前编码的项目管理员已经创建，如果需要请向管理员申请权限！！");
        project = angelaProjectRepository.save(project);
        if (project.getId() !=null){
            resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
            resultMessage.setId(project.getId()+"");
        }else{
            resultMessage.setCode(ResultCodeConstant.SERVICE_ERROR_CODE);
        }
        return resultMessage;
    }

    public ResultMessage removeByParentId(String parentId) {
        ResultMessage resultMessage = new ResultMessage();
        Sort sort = new Sort(Sort.Direction.DESC, "createdDate");
        List<AngelaLoggerProject> children = angelaProjectRepository.findByParentNode(parentId, sort);
        for(AngelaLoggerProject loggerProject : children){
            angelaProjectRepository.delete(loggerProject.getId());
        }
        angelaProjectRepository.delete(parentId);
        return  resultMessage;
    }

}
